package org.micode.demo.db.domain.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.micode.common.validater.InsertGroup;
import org.micode.common.validater.UpdateGroup;
import org.micode.common.validater.EnumValidation;
import org.micode.common.exception.VerifyFailException;
import org.micode.common.utils.StringExtUtils;
import org.micode.common.domain.base.entity.BaseDelUpdEntity;

import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.PositiveOrZero;
import javax.validation.constraints.Size;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 系统用户实体类
 *
 * @author: wutianbin
 * @version: v1.0.27
 * @CreateDate: 2021-11-10
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class SysUserDO extends BaseDelUpdEntity<Long> {

    /** 用户ID，Long */
    @NotNull(message = "用户ID,不能为空", groups = UpdateGroup.class)
    @PositiveOrZero(message = "用户ID,不能为负数", groups = {InsertGroup.class, UpdateGroup.class})
    private Long userId;

    /** 登录账号，String */
    @NotNull(message = "登录账号,不能为空", groups = InsertGroup.class)
    @Size(max = 32,message = "登录账号,最大长度32个字", groups = {InsertGroup.class, UpdateGroup.class})
    private String loginName;

    /** 密码，String */
    @JsonIgnore
    @NotNull(message = "密码,不能为空", groups = InsertGroup.class)
    @Size(max = 128,message = "密码,最大长度128个字", groups = {InsertGroup.class, UpdateGroup.class})
    private String password;

    /** 加密盐，String */
    @JsonIgnore
    @NotNull(message = "加密盐,不能为空", groups = InsertGroup.class)
    @Size(max = 32,message = "加密盐,最大长度32个字", groups = {InsertGroup.class, UpdateGroup.class})
    private String salt;

    /** 用户名，String */
    @NotNull(message = "用户名,不能为空", groups = InsertGroup.class)
    @Size(max = 32,message = "用户名,最大长度32个字", groups = {InsertGroup.class, UpdateGroup.class})
    private String userName;

    /** 联系电话，String */
    @Size(max = 16,message = "联系电话,最大长度16个字", groups = {InsertGroup.class, UpdateGroup.class})
    private String phone;

    /** 默认标识, 枚举类型: DefaultFlagEnum，Integer */
    private Integer defaultFlag;

    /** 用户类型, 枚举类型: UserTypeEnum，Integer */
    private Integer userType;

    /** 排序号，Integer */
    private Integer sortNo;

    /** 禁用标识, 枚举类型: DisableFlagEnum，Integer */
    private Integer disableFlag;

    /** 备注说明，String */
    @Size(max = 512,message = "备注说明,最大长度512个字", groups = {InsertGroup.class, UpdateGroup.class})
    private String description;

    @JsonIgnore
    @Override
    public Long getKeyValue() {
        return userId;
    }

    @Override
    public void checkBeforeInsert() {
        if (StringExtUtils.isAnyEmpty(loginName)) {
            throw new VerifyFailException("SysUserDO.loginName 必须非空");
        }
        if (StringExtUtils.isAnyEmpty(password)) {
            throw new VerifyFailException("SysUserDO.password 必须非空");
        }
        if (StringExtUtils.isAnyEmpty(salt)) {
            throw new VerifyFailException("SysUserDO.salt 必须非空");
        }
        if (StringExtUtils.isAnyEmpty(userName)) {
            throw new VerifyFailException("SysUserDO.userName 必须非空");
        }
    }

    @Override
    public String toString() {
        return "userId=" + userId +
                ", loginName='" + loginName + "'" +
                ", password='" + password + "'" +
                ", salt='" + salt + "'" +
                ", userName='" + userName + "'" +
                ", phone='" + phone + "'" +
                ", defaultFlag=" + defaultFlag +
                ", userType=" + userType +
                ", sortNo=" + sortNo +
                ", disableFlag=" + disableFlag +
                ", description='" + description + "'" +
                ", " + super.toString();
    }

}
